package log_stash

import (
	"encoding/json"
	"time"
)

type LogStashModel struct {
	ID        uint      `gorm:"primaryKey" json:"id"` // 主键id
	CreatedAt time.Time `json:"created_at"`           // 创建时间
	UpdatedAt time.Time `json:"-"`                    // 更新时间
	IP        string    `gorm:"size:32" json:"ip"`
	Addr      string    `gorm:"size:64" json:"addr"`
	Level     Level     `gorm:"size:4" json:"level"`     // 日志等级
	Content   string    `gorm:"size:128" json:"content"` // 日志内容
	UserID    uint      `json:"user_id"`                 // 登录用户的关联
}

func (LogStashModel) TableName() string {
	return "log_stash"
}

type Level int

const (
	DebugLevel Level = 1
	InfoLevel  Level = 2
	WarnLevel  Level = 3
	ErrorLevel Level = 4
)

// 这个方法是数据返回给前端需要序列化会知道
func (lv Level) MarshalJSON() ([]byte, error) {
	return json.Marshal(lv.String())
}

func (lv Level) String() string {
	var str string
	switch lv {
	case DebugLevel:
		str = "debug"
	case InfoLevel:
		str = "info"
	case WarnLevel:
		str = "warn"
	case ErrorLevel:
		str = "error"
	default:
		str = "其他"
	}
	return str
}
